package com.publicprintingwithjason.gui;




import java.awt.Point;
import java.util.ArrayList;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Classe responsavel por gerenciar as posicoes dos agentes e impressoras
 * @author azul
 */
public class Waypoints{
    private Point[][] wp;
    private Point[][] wpPrinters;
    private Point[] wpSuplyMan;
    
    private static Waypoints instance;

    public static Waypoints getWP(){
        if(instance == null){
            instance = new Waypoints();
        }
        return instance;
    }

    private Waypoints() {
        // Waypoints dos agentes
        wp  = new Point[9][8];
        for(int i=0; i < 8; i++){
            wp[0][i] = new Point(i*100, 0);
            wp[1][i] = new Point(i*100, 75*1);
            wp[2][i] = new Point(i*100, 75*2);
            wp[3][i] = new Point(i*100, 75*3);
            wp[4][i] = new Point(i*100, 75*4);
            wp[5][i] = new Point(i*100, 75*5);
            wp[6][i] = new Point(i*100, 75*6);
            wp[7][i] = new Point(i*100, 75*7);
            wp[8][i] = new Point(i*100, 75*7+50);
        }
        // Posicoes das impressoras
        wpPrinters = new Point[10][2];
        wpPrinters[0][0] = new Point(25,25);
        wpPrinters[0][1] = new Point(0,0);
        wpPrinters[1][0] = new Point(25,475);
        wpPrinters[1][1] = new Point(0,450);
        wpPrinters[2][0] = new Point(150,200);
        wpPrinters[2][1] = new Point(200,225);
        wpPrinters[3][0] = new Point(250,325);
        wpPrinters[3][1] = new Point(300,300);
        wpPrinters[4][0] = new Point(350,50);
        wpPrinters[4][1] = new Point(400,75);
        wpPrinters[5][0] = new Point(350,475);
        wpPrinters[5][1] = new Point(400,450);
        wpPrinters[6][0] = new Point(425,175);
        wpPrinters[6][1] = new Point(400,150);
        wpPrinters[7][0] = new Point(550,325);
        wpPrinters[7][1] = new Point(600,300);
        wpPrinters[8][0] = new Point(750,50);
        wpPrinters[8][1] = new Point(800,75);
        wpPrinters[9][0] = new Point(625,475);
        wpPrinters[9][1] = new Point(600,450);
        //Posicoes do responsavel pelos suprimentos
        wpSuplyMan = new Point[3];
        wpSuplyMan[0] = new Point(775,550);
        wpSuplyMan[1] = new Point(775,525);
        wpSuplyMan[2] = new Point(800,525);
    }

    public Point[][] getWayPoints() {
        return wp;
    }

    public Point[][] getWpPrinters() {
        return wpPrinters;
    }
    
    public Point[] getWpSuplyMan(){
    	return wpSuplyMan;
    }

    // Gera um caminho entre dois pontos
    public ArrayList<Point> getPath(Point p1, Point p2){
        ArrayList<Point> path = new ArrayList<Point>();
        path.add(new Point(p2.x, p1.y));
        path.add(new Point(p2.x, p2.y));
        return path;
    }

    // Encontra o elemento da matriz de wps para uma dada posicao
    public Point get(Point position){
        // Encontra o elemento da matriz de wp correspondente às coordenadas dadas
        for(int i=0; i < wp.length; i++){
            for(int j=0; j < wp[0].length; j++){
                if(wp[i][j].x == position.x && wp[i][j].y == position.y){
                    return new Point(j, i);
                }
            }
        }
        return null;
    }

    // Método para encontrar os vizinhos de um dado wp
    public ArrayList<Point> getNeighbors(Point position){
        Point element = get(position);
        // Se nada corresponder à posição, siginifica que as coordenadas não pertencem
        // à matriz de wp e, portanto, não possui vizinhos
        if(element == null){
            return null;
        }
        // Caso contrário, gera a lista de vizinhos
        ArrayList<Point> neighbors = new ArrayList<Point>();
        // Vizinhos horizontais
        if(element.x == 0){
            neighbors.add(wp[element.y][1]);
        }
        else if(element.x == wp[0].length-1){
            neighbors.add(wp[element.y][element.x-1]);
        }
        else{
            neighbors.add(wp[element.y][element.x-1]);
            neighbors.add(wp[element.y][element.x+1]);
        }
        // Vizinhos verticais
        if(element.y == 0){
            neighbors.add(wp[1][element.x]);
        }
        else if(element.y == wp.length-1){
            neighbors.add(wp[element.y-1][element.x]);
        }
        else{
            neighbors.add(wp[element.y-1][element.x]);
            neighbors.add(wp[element.y+1][element.x]);
        }
        return neighbors;
    }
}
